from core.aparser import Parser
from core.printer import ASTPrinter
from core.ascanner import Scanner

prompt = "scheme"


def eval(code: str):
    scanner = Scanner(code)
    tokens = scanner.scan_tokens()
    parser = Parser()
    asts = parser.parse(tokens)
    printer = ASTPrinter()
    for ast in asts:
        print(printer.format(ast))


def repl():
    content = ''
    print(prompt, end='>')
    while True:
        line = input()
        if line.endswith("\\"):
            content += line[:-1]
            continue
        else:
            content += line
        try:
            content = content.strip()
            if len(content) > 0:
                eval(content)
        except Exception as e:
            print(e)

        content = ''
        print(prompt, end='>')


def main():
    repl()


if __name__ == '__main__':
    main()
